<?php
/**
 * gallerycontroller.php
 * 
 * Create on 2015-04-02
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\index;

use haibao\cms\business\control\PublicController as PublicControllerBusiness;
use \haibao\cms\business\control\pc\action\ArticleAction;
use \haibao\cms\business\control\pc\entity\ImageCollectiveEntity as ImageCollectiveEntity;
use \haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\web\view\app\article\Preview;

class GalleryController extends IndexController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$dataList = $this->getPicData();
		
		$this->assign('dataList', json_decode(json_encode($dataList)));
		$this->assign('picUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC));
		
		$path = $this->generateHtmlPath(__CLASS__);
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		
		return $content;
	}
	
	/**
	 * 文章图集数据
	 * 3天内范围内，全站3级以上文章图集 发布时间正序。
	 */
	public function getPicData(){
	    $articleEntity = new ArticleEntity();
	    $articleEntity->setLevel(3);
	    $articleEntity->setBigLevel(7);
	    $articleEntity->setOffset(50);
	    $articleEntity->setLimit(8);
	    $articleEntity->setIsDate(true,6);
	    $articleEntity->setIsProImage(true);
	    $articleEntity->setIsLeftJoinTag(true);
	    $articleEntity->setIsPrimaryTag(true);
	    $articleEntity->setIsConditionTag(false);
	    $articleEntity->setIsReturnTag(true);
	    $articleEntity->setIsImageColl(true);
	    $articleEntity->setOrderByNew(true,'desc');
	    $articleEntity->setSource(__CLASS__);
	    $articleEntity->setIsDebug(false);
	    $articleEntity->setIsOnlyReturnPrimaryTag(true);
	    $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
	    $this->articleAction->initArticleAction();
	    $this->articleAction->setArticleEnity($articleEntity);
	    list($articles,$imageColls) = $this->articleAction->getListForNew();
	    $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$articleEntity->getLimit());
	    $topTag = self::getTopTags();
	    foreach($mergeData as $key=>$article){
	        if($article->SiteType == \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_ARTICLE){//文章
	            if(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
	                $article->AccessUrl = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
	            }else {
	                $article->AccessUrl = $articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];  //图片地址
	            }
	            $article->ArticleUrl = $this->getArticleUrl($article->PublishId,$article->ArticleTag->ParentId,\haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_ARTICLE_PIC); //文章地址
	            
	            if(isset($articles['tag'][$article->ArticleTag->TagId])){
	                $article->ArticleTag->TagName = $articles['tag'][$article->ArticleTag->TagId]['TagName'];
	                $article->ArticleTag->TagUrl = $articles['tag'][$article->ArticleTag->TagId]['Url']; //tagURl
	            }
	        }else{//图集
                if(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_FOUR])){
                    $article->AccessUrl = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_FOUR];
                }else {
                    $article->AccessUrl = $imageColls['proImage'][$article->Id][\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO]; //图片地址
                }
                //$article->ImageCollectivePro = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.$topTag[$article->ImageCollectiveTag->ParentId]['channel'].'/pics/'.$article->ImageCollectiveTag->ImageCollectiveId.'.htm';
                $article->ImageCollectivePro = $this->getArticleUrl($article->ImageCollectiveTag->ImageCollectiveId,$article->ImageCollectiveTag->ParentId,\haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_IMAGE);
                if(isset($imageColls['tag'][$article->ImageCollectiveTag->TagId])){
                    $article->ImageCollectiveTag->TagName = $imageColls['tag'][$article->ImageCollectiveTag->TagId]['TagName'];
                    $article->ImageCollectiveTag->Url = $imageColls['tag'][$article->ImageCollectiveTag->TagId]['Url'];
                }
            }
	         
	    }
	    $articleLists = $temp = array();
	    if($mergeData){
            foreach($mergeData as $key=>$val){
                if($val->SiteType == \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_IMAGE){
                    $temp['Id'] = $val->Id;
                    $temp['ChannelUrl'] = $val->ImageCollectivePro;
                    $temp['TagName'] = $val->ImageCollectiveTag->TagName;
                    $temp['TagUrl'] =  $val->ImageCollectiveTag->Url;
                    $temp['ProImage'] = $val->AccessUrl;
                    $temp['Title'] = $val->Title;
                    $temp['SiteType'] = \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_IMAGE;
                }else{
                    $temp['Id'] = $val->Id;
                    $temp['ChannelUrl'] = $val->ArticleUrl;
                    $temp['TagName'] = $val->ArticleTag->TagName;
                    $temp['TagUrl'] =  $val->ArticleTag->TagUrl;
                    $temp['ProImage'] = $val->AccessUrl;
                    $temp['Title'] = $val->Title;
                    $temp['SiteType'] = \haibao\cms\business\control\pc\action\ArticleAction::INFO_TYPE_ARTICLE;
                }
                array_push($articleLists,$temp);
            }
	    }
	    return $articleLists;
	}
}